package com.bao.work.work02;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author baoye
 * @date 2024-08-14/19:05
 */
public class Test02 {
    @Test
    public void test01() {
        List<Student> students = new ArrayList<>();
        //添加学生到集合
        try {
            students.add(new Student("08001", 32, "0800101", "照明", new SimpleDateFormat("yyyy-MM-dd").parse("1985-12-23")));
            students.add(new Student("08002", 28, "0800201", "胡光辉", new SimpleDateFormat("yyyy-MM-dd").parse("1985-10-05")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //创建并添加内容到xml;
        Document document = DocumentHelper.createDocument();
        Element studentsT = document.addElement("students");
        for (Student student : students) {
            Element studentT = studentsT.addElement("student");
            studentT.addElement("classId").addText(student.getClassId());
            studentT.addElement("stuCount").addText(String.valueOf(student.getStuCount()));
            studentT.addElement("stuId").addText(student.getStuId());
            studentT.addElement("name").addText(student.getName());
            studentT.addElement("birth").addText(new SimpleDateFormat("yyyy-MM-dd").format(student.getBirth()));
        }
        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        outputFormat.setEncoding("UTF-8");
        XMLWriter xmlWriter = null;
        try {
            xmlWriter = new XMLWriter(new FileWriter("src/com/bao/work/work02/students.xml"), outputFormat);
            xmlWriter.write(document);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                xmlWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test02() {
        //获取xml中的数据,使用反射创建对象
        List<Student> studentList = new ArrayList<>();

        try {
            SAXReader saxReader = new SAXReader();
            File file = new File("src/com/bao/work/work02/students.xml");
            Document read = saxReader.read(file);
            Element rootStudents = read.getRootElement();
            List<Element> students = rootStudents.elements();

            for (Element student : students) {
                Class<Student> studentClass = Student.class;
                Student typeStudent = studentClass.newInstance();
                for (Element studentMain : student.elements()) {
                    String key = studentMain.getName();
                    String value = studentMain.getText();
                    Field field = studentClass.getDeclaredField(key);
                    field.setAccessible(true);
                    if (field.getGenericType().getTypeName().contains("String")) {
                        field.set(typeStudent, value);
                    } else if (field.getGenericType().getTypeName().contains("Integer")) {
                        field.set(typeStudent, Integer.parseInt(value));
                    } else if (field.getGenericType().getTypeName().contains("Date")) {
                        field.set(typeStudent, new SimpleDateFormat("yyyy-MM-dd").parse(value));
                    }
                }
                studentList.add(typeStudent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Student student : studentList) {
            System.out.println(student);
        }

    }
}
